<?php

/**
 *DOESN'T match label with correct comment if there is different number of
 *labels and comments. e.g.: MUSIC ONTOLOGY...
 *See here: http://xmlns.com/foaf/spec/ "Status of this document" then click
 *on content negotiation for POSSIBLE info on how to parse FOAF (when its not down...)
 *THIS IS A TESTING FILE.
 */

/* known parents
  rdf:Property (DC and FOAF)
  owl:Description
*/

$DC = "http://dublincore.org/2010/10/11/dcelements.rdf";
$FOAF_WEBSITE = "http://xmlns.com/foaf/spec/index.rdf";
$MUSIC_ONTOLOGY = "http://motools.sourceforge.net/doc/musicontology.rdfs";
$GEO_NAMES = "http://www.geonames.org/ontology/ontology_v2.2.1.rdf";
$GEO_POSITION = "http://www.w3.org/2003/01/geo/wgs84_pos.rdf";
$VCARD = "http://www.w3.org/2006/vcard/ns.rdf";
$FOAF = "foaf.rdf";


// name, description, propertyURI, namespace, rdfFile

$xmlDoc = new DOMDocument();
$xmlDoc->load($FOAF);
$xpath = new DOMXPath($FOAF);

/*
$parentOWL = $xpath->query("//owl:ObjectProperty");
$parentRDF = $xpath->query("//rdf:Property");

if ($parentOWL == null and $parentRDF != null) {
    $parents = $parentRDF;
}
elseif ($parentRDF == null && $parentOWL != null) {
    $parents = $parentOWL;
}
if ($parentRDF != null and $parentOWL != null) {
    if (($parentRDF->length) > ($parentOWL->length)) {
        $parents = $parentRDF;
    } else {
        $parents = $parentOWL;
    }
}
*/

$properties = $xpath->query("//@rdf:about");
$xmlBase = $xpath->query("//@xml:base");
/*
foreach ($properties as $property) {
    echo("PROPERTY: " . $property->parentNode->nodeName . "<br />");
}*/
if ($xmlBase->item(0)->nodeValue == null) {
    // change this to ontology namespace from db...
    $base = "http://xmlns.com/foaf/0.1/";
}
else {
    $base = $xmlBase->item(0)->nodeValue;
}

foreach ($properties as $property) {
    $stop = false;
    if (strstr($property->nodeValue,'http')) {
        if (!strstr($property->nodeValue, $base)) {
            // Case where property is different ontology. Do nothing.
            $stop = true;
        }
    }
    if (strstr($property->parentNode->nodeName,'Class'))
    if (!$stop) {
        $label = $property->parentNode->attributes->getNamedItem("label")->nodeValue;
        $comment = $property->parentNode->attributes->getNamedItem("comment")->nodeValue;
        if ($label == null) {
            for ($i = 0; $i < $property->parentNode->childNodes->length; $i++) {
                if ($property->parentNode->childNodes->item($i)->nodeName == "rdfs:label") {
                    $label = $property->parentNode->childNodes->item($i)->nodeValue;
                }
            }
        }
        if ($comment == null) {
            for ($i = 0; $i < $property->childNodes->length; $i++) {
                if ($property->parentNode->childNodes->item($i)->nodeName == "rdfs:label") {
                    $comment = $property->parentNode->childNodes->item($i)->nodeValue;
                }
            }
        }
        echo("About: " . $property->nodeValue . "<br />");
        echo("Label: " . $label . "<br />");
        echo("Comment: " . $comment . "<br /><br />");
    }
}

// $parents = $xpath->query("//rdf:Property");

/*
for ($i = 0; $i < $parents->length; $i++) {
    echo("PARENTS: " . $parents->item($i)->nodeName . "<br />");
    
    echo("Label: " . $parents->item($i)->attributes->getNamedItem("label")->nodeValue . "<br />");
    echo("Comments: " . $parents->item($i)->attributes->getNamedItem("comment")->nodeValue . "<br />");
}
*/
/*
foreach ($parents as $parent) {
    $label = $parent->attributes->getNamedItem("label")->nodeValue;
    $comment = $parent->attributes->getNamedItem("comment")->nodeValue;
    if ($label == null) {
        for ($i = 0; $i < $parent->childNodes->length; $i++) {
            if ($parent->childNodes->item($i)->nodeName == "rdfs:label") {
                $label = $parent->childNodes->item($i)->nodeValue;
            }
            elseif ($parent->childNodes->item($i)->nodeName == "rdfs:comment") {
                $comment = $parent->childNodes->item($i)->nodeValue;
            }
        }
    }
    echo("About: " . $label . "<br />");
    echo("Comment: " . $comment . "<br />");
}
*/

echo("WTF");

/*

$parentPath = "//rdfs:label/..";
$labelPath = "rdfs:label";
$commentPath = "rdfs:comment";

$lTest = $xpath->query($parentPath . $labelPath);

$parents = $xpath->query("//rdfs:label/..");
if(!($xpath->query("//rdfs:label/..")->length > $xpath->query("//@rdfs:label/..")->length)) {
    $parents = $xpath->query("//@rdfs:label/..");
}
// nested tag version
$nestedLabels = $xpath->query("//rdfs:label");
$nestedComments = $xpath->query("//rdfs:comment");
// attribute version
$attributeLabels = $xpath->query("//@rdfs:label");
$attributeComments = $xpath->query("//@rdfs:comment");

$labels = $nestedLabels;
$comments = $nestedComments;

// determine if file is using nested tags or attributes to define labels/comments
if ($attributeLabels->length > $nestedLabels->length) {
    $labels = $attributeLabels;
}
if ($attributeComments->length > $nestedComments->length) {
    $comments = $attributeComments;
}

echo("Parents Length: " . $parents->length . "<br />");

foreach ($parents as $parent) {
    echo("URI thing: " . $parent->nodeValue."<br />");
}

$group = array();

$item = array();

echo($labels->length."<br />");
echo($comments->length."<br />");

for ($i = 0; $i < $labels->length; $i++) {
    echo("NEXT SIBLING: " . $labels->item($i)->nextSibling->nodeValue . "<br />");
    echo $labels->item($i)->nodeValue." ";
    echo $comments->item($i)->nodeValue."<br />";
}*/

?> 